import {AsyncStorage} from 'react-native'
import T from "../utils/T";
import Request from "../utils/Request";

const {Toast} = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_search";
export default {
    namespace: 'search',
    state: {
        loading: false,
        activities: [],
        companies: [],
        groups: [],
        member: []

    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {


        // 保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        // 本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        * fetchData({payload}, {call, put, select}) {
            Toast.loading(T('loading'));
            const auth: AuthModel = yield select(state => state.auth);
            payload['Authorization'] = `${auth.token_type} ${auth.access_token}`;
            const res = yield call(Request.get, Request.getApi().search, payload);
            Toast.hide();
            if (res) {
                yield put({
                    type: 'save',
                    payload: {...res}
                })
            }
        },
    },
    subscriptions: {
        setup({dispatch}) {

        }
    },
};
